<template>
  <v-data-table
    :headers="headers"
    :items="desserts"
    sort-by="calories"
    class="elevation-1"
    :loading="loading"
    loading-text="Loading... Please wait"
  >
    <template v-slot:top>
      <v-toolbar flat>
        <v-toolbar-title>文章管理</v-toolbar-title>
        <v-divider class="mx-4" inset vertical></v-divider>
        <v-spacer></v-spacer>
        <v-dialog v-model="dialog" max-width="500px">
          <template v-slot:activator="{ attrs }">
            <v-btn color="primary" class="mb-2" v-bind="attrs" @click="addItem">
              新建文章
            </v-btn>
          </template>
          <v-card>
            <v-card-title>
              <span class="text-h5">{{ formTitle }}</span>
            </v-card-title>

            <v-card-text>
              <v-container>
                <v-row>
                  <v-col cols="12" sm="6" md="4">
                    <v-text-field
                      v-model="editedItem.name"
                      label="Dessert name"
                    ></v-text-field>
                  </v-col>
                  <v-col cols="12" sm="6" md="4">
                    <v-text-field
                      v-model="editedItem.calories"
                      label="Calories"
                    ></v-text-field>
                  </v-col>
                  <v-col cols="12" sm="6" md="4">
                    <v-text-field
                      v-model="editedItem.fat"
                      label="Fat (g)"
                    ></v-text-field>
                  </v-col>
                  <v-col cols="12" sm="6" md="4">
                    <v-text-field
                      v-model="editedItem.carbs"
                      label="Carbs (g)"
                    ></v-text-field>
                  </v-col>
                  <v-col cols="12" sm="6" md="4">
                    <v-text-field
                      v-model="editedItem.protein"
                      label="Protein (g)"
                    ></v-text-field>
                  </v-col>
                </v-row>
              </v-container>
            </v-card-text>

            <v-card-actions>
              <v-spacer></v-spacer>
              <v-btn color="blue darken-1" text @click="close"> 关闭 </v-btn>
              <v-btn color="blue darken-1" text @click="save"> 保存 </v-btn>
            </v-card-actions>
          </v-card>
        </v-dialog>
        <v-dialog v-model="dialogDelete" max-width="500px">
          <v-card>
            <v-card-title class="text-h5">是否需要删除记录?</v-card-title>
            <v-card-actions>
              <v-spacer></v-spacer>
              <v-btn color="blue darken-1" text @click="closeDelete"
                >关闭</v-btn
              >
              <v-btn color="blue darken-1" text @click="deleteItemConfirm"
                >删除</v-btn
              >
              <v-spacer></v-spacer>
            </v-card-actions>
          </v-card>
        </v-dialog>
      </v-toolbar>
    </template>
    <template v-slot:item.actions="{ item }">
      <v-icon small class="mr-2" @click="editItem(item)"> mdi-pencil </v-icon>
      <v-icon small @click="deleteItem(item)"> mdi-delete </v-icon>
    </template>
    <template v-slot:no-data> </template>
  </v-data-table>
</template>

<script>
import api from "../../axios/api.js";
export default {
  data: () => {
    return {
      dialog: false,
      dialogDelete: false,
      headers: [
        { text: "文章标题", value: "title" },
        { text: "描述", value: "desc" },
        { text: "封面图", value: "coverUrl" },
        { text: "创建时间", value: "ctime" },
        { text: "修改时间", value: "utime" },
        { text: "操作", value: "actions", sortable: false },
      ],
      desserts: [],
      editedIndex: -1,
      editedItem: {
        title: "",
        desc: "",
        coverUrl: "",
        utime: "",
        ctime: "",
      },
      defaultItem: {
        title: "",
        desc: "",
        coverUrl: "",
        utime: "",
        ctime: "",
      },
      loading: false,
    };
  },

  computed: {
    formTitle() {
      return this.editedIndex === -1 ? "创建" : "编辑";
    },
  },

  watch: {
    dialog(val) {
      val || this.close();
    },
    dialogDelete(val) {
      val || this.closeDelete();
    },
  },

  created() {
    this.initialize();
  },

  methods: {
    initialize() {
      this.loading = true;
      api
        .getAllWorks()
        .then((res) => {
          console.log(res);
          this.desserts = res.data.data;
          this.loading = false;
        })
        .catch((e) => {
          console.error(e), (this.loading = false);
        });
    },

    editItem(item) {
      this.$router.push(`/create/${item.id}`);
    },

    addItem() {
      this.$router.push(`/create/${-1}`);
    },

    deleteItem(item) {
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialogDelete = true;
    },

    deleteItemConfirm() {
      api
        .deleteWorkById({ id: this.desserts.splice(this.editedIndex, 1)[0].id })
        .then()
        .catch((e) => {
          console.error(e);
        })
        .finally((this.dialogDelete = false));
    },

    close() {
      this.dialog = false;
      this.$nextTick(() => {
        this.editedItem = Object.assign({}, this.defaultItem);
        this.editedIndex = -1;
      });
    },

    closeDelete() {
      this.dialogDelete = false;
      this.$nextTick(() => {
        this.editedItem = Object.assign({}, this.defaultItem);
        this.editedIndex = -1;
      });
    },

    save() {
      if (this.editedIndex > -1) {
        console.log("创建");
      } else {
        console.log("修改");
      }
      this.close();
    },
  },
};
</script>

<style>
</style>